package com.hyving.home.jpademo.cron;

import com.alibaba.fastjson.JSON;
import com.hyving.home.jpademo.model.Student;
import com.hyving.home.jpademo.repository.StudentDao;
import com.hyving.home.jpademo.repository.TeacherDao;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DataTestRunner  implements ApplicationRunner {


    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        Student s =new Student();
        s.setId(1l);
        s.setName("huang");
        s.setAge(30);
        s.setSex("男");
        studentDao.save(s);

        Student huangs = studentDao.findStudentByNameIs("huang");
        System.out.println(JSON.toJSONString(huangs));

        //分页
        int page=1,size=10;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Student> all = studentDao.findAll(pageable);
        System.out.println(JSON.toJSONString(all));
        Page<Student> huang = studentDao.findByName("huang", pageable);
        System.out.println(JSON.toJSONString(huang.getContent()));
    }
}
